home *** CD-ROM | disk | FTP | other *** search
/ Delphi 2.0 - Programmer's Utilities Power Pack / Delphi 2.0 Programmer's Utilities Power Pack.iso / s_to_z / tpack / userwin.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-09-15  |  11.2 KB  |  331 lines

  1. unit UserWin;
  2.  
  3. {-----------------------------------------------------------------------------------------}
  4. { USERWIN                                                                                 }
  5. {-----------------------------------------------------------------------------------------}
  6.  
  7. interface
  8.  
  9. uses
  10.   Classes,
  11.   UserInfo;
  12.  
  13. function TrailingChar(Value:String;Trailer:Char):String; {insures a trailing character}
  14. function TrailingBackSlash(Value:String):String;         {insures a trailing '\'}
  15.  
  16. Type
  17.   TWindowsUserInfo = class(TUserInfo)
  18.   {service component to get some windows info as well as unique files that can be automatically
  19.   zapped when the component shuts down. it can also validate a password against the screen saver}
  20.   private
  21.     fUserName,
  22.     fCompanyName,
  23.     fPassWord       : PString;
  24.     fSsDelay        : Integer;
  25.     fZap            : Boolean;
  26.     fUniqueNames    : TStringList;
  27.   protected
  28.     procedure WinEncrypt(Strg: PChar);
  29.     Procedure EncryptCString(S: PChar);
  30.     Function  EncryptString(const S: String): String;
  31.     function GetUserName:String;
  32.     function GetCompanyName:String;
  33.     function GetWindowsPath:String;
  34.     function GetSystemPath:String;
  35.     function GetFreeGDI: integer;
  36.     function GetFreeUser: integer;
  37.     function GetFreeSystem: integer;
  38.     function GetUniqueFileName:String;
  39.     function GetFreeSpace: longint;
  40.     procedure SetNoLongInt(Value:LongInt);
  41.     procedure SetNoInteger(Value:Integer);
  42.     procedure SetNoString(const Value:String);
  43.   public
  44.     Constructor Create(aOwner:TComponent); Override;
  45.     Destructor Destroy; Override;
  46.     function UpdateOK: boolean; Override;
  47.     Function HasPassWord:Boolean;
  48.     Function CheckPassWord(const Value:String):Boolean;
  49.     property UniqueFileName: String read GetUniqueFileName;
  50.   published
  51.     property ZapUniqueOnFree:Boolean read fZap write fZap default true;
  52.     property UserName: String read GetUserName write SetNoString stored false;
  53.     property CompanyName: String read GetCompanyName write SetNoString stored false;
  54.     property SaverDelay: Integer read fssDelay write SetNoInteger stored false;
  55.     property WindowsPath: String read GetWindowsPath write SetNoString stored false;
  56.     property SystemPath: String read GetSystemPath write SetNoString stored false;
  57.     property FreeSpace: Longint read GetFreeSpace write SetNoLongInt stored false;
  58.     property FreeGDI: integer read GetFreeGDI write SetNoInteger stored false;
  59.     property FreeUser: integer read GetFreeUser write SetNoInteger stored false;
  60.     property FreeSystem: integer read GetFreeSystem write SetNoInteger stored false;
  61.     end;
  62.  
  63. implementation
  64.  
  65. uses
  66.   IniFiles
  67.   ,PasUtils
  68.   ,WinTypes
  69.   ,WinProcs
  70.   ,Controls
  71.   ,SysUtils;
  72.  
  73. const
  74.   BufSize = 144;
  75.  
  76. {------------------------------------------------------------------------------}
  77. { TRAILING CHARACTER, TRAILING BACKSLASH                                       }
  78. {------------------------------------------------------------------------------}
  79. {need to include a StringServices component perhaps} {for now these utils are here.}
  80.  
  81. function TrailingChar(Value:String;Trailer:Char):String; {insures a trailing character}
  82. begin
  83.   Result:=Value;
  84.   if copy(Value,length(Value),1)<>Trailer then
  85.     Result:=Result+Trailer;
  86. end;
  87.  
  88. function TrailingBackSlash(Value:String):String; {insures a trailing '\'}
  89. begin
  90.   if Value<>'' then
  91.     Result:=TrailingChar(Value,'\')
  92.   else
  93.     Result:=Value;
  94. end;
  95.  
  96. {-----------------------------------------------------------------------------------------}
  97. { OBJECT CREATION                                                                         }
  98. {-----------------------------------------------------------------------------------------}
  99.  
  100. Constructor TWindowsUserInfo.Create(aOwner:TComponent);
  101. begin
  102.   inherited Create(aOwner);
  103. {  Options:=[uifUpdateOnLoad,uifUpdateOnGet];  }
  104.   fUserName:=NullStr;
  105.   fCompanyName:=NullStr;
  106.   fPassWord:=NullStr;
  107.   fUniqueNames:=TStringList.Create;
  108.   fZap:=True;
  109. end;
  110.  
  111. Destructor TWindowsUserInfo.Destroy;
  112. var
  113.   i,n:longint;
  114. begin
  115.   with fUniqueNames do begin
  116.     n:=Count-1;
  117.     if fZap and (n>-1) then
  118.       for i:=0 to n do
  119.         if FileExists(Strings[i]) then
  120.           DeleteFile(Strings[i]);
  121.     Free;
  122.     end;
  123.   DisposeStr(fUserName);
  124.   DisposeStr(fCompanyName);
  125.   DisposeStr(fPassWord);
  126.   inherited Destroy;
  127. end;
  128.  
  129. function TWindowsUserInfo.UpdateOK: boolean;
  130. var
  131.   Ini:TIniFile;
  132.   fileHandle: THandle;
  133.   zStr:PChar;
  134. begin
  135.   Result:=inherited UpdateOK;
  136.   if not Result then
  137.     Exit;
  138.   Ini := TIniFile.Create('CONTROL.INI');                         { Open the Ini File }
  139.   AssignStr(fPassword,Ini.ReadString('ScreenSaver','Password',''));{ Read the Password }
  140.   Ini.Free;                                                      { Close It }
  141.   SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT,0,@fSsDelay,0);  { Read the Delay }
  142.   if fSsDelay > 0 then fSsDelay := fSsDelay Div 60;              { Get Minutes }
  143.   if fSsDelay = 0 then fSsDelay := 1;                            { JIC an awkward Number }
  144.   { Get user name and company name }                               {what did he mean there?}
  145.   fileHandle := LoadLibrary('USER');
  146.   if fileHandle >= HINSTANCE_ERROR then begin
  147.     zStr:=MakePChar('');
  148.     If LoadString(fileHandle, 514, zStr, 255) <> 0 Then
  149.       AssignStr(fUserName,StrPas(zStr));
  150.     If LoadString(fileHandle, 515, zStr, 255) <> 0 Then
  151.       AssignStr(fCompanyName,StrPas(zStr));
  152.     FreeLibrary(fileHandle);
  153.     end;
  154. end;
  155.  
  156. {-----------------------------------------------------------------------------------------}
  157. { OBJECT PLUMBING                                                                         }
  158. {-----------------------------------------------------------------------------------------}
  159.  
  160. procedure TWindowsUserInfo.SetNoLongInt(Value:LongInt);
  161. begin
  162. end;
  163.  
  164. procedure TWindowsUserInfo.SetNoInteger(Value:Integer);
  165. begin
  166. end;
  167.  
  168. procedure TWindowsUserInfo.SetNoString(const Value:String);
  169. begin
  170. end;
  171.  
  172. function TWindowsUserInfo.GetWindowsPath:String;
  173. var
  174.   Buffer: PChar;
  175.   Count: Word;
  176. begin
  177.   GetMem(Buffer, BufSize);
  178.   Count:=GetWindowsDirectory(Buffer,BufSize);
  179.   Result:=strpas(Buffer);
  180.   FreeMem(Buffer, BufSize);
  181.   Result:=TrailingBackSlash(Result);
  182. end;
  183.  
  184. function TWindowsUserInfo.GetSystemPath:String;
  185. var
  186.   Buffer: PChar;
  187.   Count: Word;
  188. begin
  189.   GetMem(Buffer, BufSize);
  190.   Count:=GetSystemDirectory(Buffer,BufSize);
  191.   Result:=strpas(Buffer);
  192.   FreeMem(Buffer, BufSize);
  193.   Result:=TrailingBackSlash(Result);
  194. end;
  195.  
  196. function TWindowsUserInfo.GetUserName:String;
  197. begin
  198.   Result:=fUserName^;
  199. end;
  200.  
  201. function TWindowsUserInfo.GetCompanyName:String;
  202. begin
  203.   Result:=fCompanyName^;
  204. end;
  205.  
  206. function TWindowsUserInfo.GetFreeSpace: longint;
  207. begin
  208.   Result:=WinProcs.GetFreeSpace(0);
  209. end;
  210.  
  211.  
  212. function TWindowsUserInfo.GetFreeGDI: integer;
  213. begin
  214.   Result:=GetFreeSystemResources(GFSR_GdiResources);
  215. end;
  216.  
  217.  
  218. function TWindowsUserInfo.GetFreeUser: integer;
  219. begin
  220.   Result:=GetFreeSystemResources(GFSR_UserResources);
  221. end;
  222.  
  223.  
  224. function TWindowsUserInfo.GetFreeSystem: integer;
  225. begin
  226.   Result:=GetFreeSystemResources(GFSR_SystemResources);
  227. end;
  228.  
  229.  
  230. {-----------------------------------------------------------------------------------------}
  231. { OBJECT FUNCTIONS                                                                        }
  232. {-----------------------------------------------------------------------------------------}
  233.  
  234. Function TWindowsUserInfo.HasPassWord:Boolean;
  235. begin
  236.   Result:=fPassword^[0]>#0;
  237. end;
  238.  
  239. Function TWindowsUserInfo.CheckPassWord(const Value:String):Boolean;
  240. {can't be constant parameter as we use the buffer to do work with}
  241. var
  242.   Cursor:TCursor;
  243. begin
  244.   if HasPassWord then
  245.     Result:= EncryptString(UpperCase(Value))=fPassWord^
  246.   else
  247.     Result:=True;
  248. end;
  249.  
  250. function TWindowsUserInfo.GetUniqueFileName:String;
  251. {this creates a file!}
  252. {could/should add names to list and delete files on free}
  253. var
  254.   Buffer: PChar;
  255.   Count: Word;
  256. begin
  257.   GetMem(Buffer, BufSize);
  258.   Count:=GetTempFileName(#0,nil,0,Buffer);
  259.   Result:=strpas(Buffer);
  260.   FreeMem(Buffer, BufSize);
  261. end;
  262.  
  263. {-----------------------------------------------------------------------------------------}
  264. { WINDOWS SCREENSAVER PASSWORD ENCRYPTION         REPACKAGED I HOPE I DONT GET SUED!      }
  265. {-----------------------------------------------------------------------------------------}
  266.  
  267. procedure TWindowsUserInfo.WinEncrypt(Strg: PChar);
  268. var
  269.   StrgPt, Strglg : Integer;                                { Local Vars }
  270.   TheByte : Byte;                                          { Working Char }
  271.  
  272.   procedure Exor (x1: byte; var x2: byte);
  273.   const  { the last three are '[]=' - not allowed in profile string }
  274.     NotAllowed = [0..$20, $7f..$90, $93..$9f, $3d, $5b, $5d];
  275.   begin
  276.     if not ((x2 xor x1) in NotAllowed) then
  277.       x2 := x2 xor x1;
  278.   end; { Exor }
  279.  
  280. begin
  281.   StrgLg := lstrlen(Strg);                                 { Get String Length }
  282.   if (StrgLg = 0) then exit;                               { empty string => nothing to do }
  283.   AnsiUpper (Strg);                                        { capitalize the string }
  284.  
  285.   for StrgPt := 0 to StrgLg - 1 do begin                   { proceed from left to right }
  286.     TheByte := byte (Strg [StrgPt]);                       { get character to encrypt }
  287.     Exor (StrgLg, TheByte);                                { xor it using string length...}
  288.     if (StrgPt = 0) then                                   { If EOS }
  289.       Exor ($2a, TheByte)                                  {...a constant...}
  290.     else begin
  291.       Exor (StrgPt, TheByte);                              {...actual string pointer...}
  292.       Exor (byte (Strg [StrgPt-1]), TheByte);              {...previous character }
  293.       end;
  294.     Strg [StrgPt] := char (TheByte);                       { store encrypted byte back }
  295.     end; { for };
  296.  
  297.   if (StrgLg > 1) then                                     { no second pass for one-byte-strings }
  298.     for StrgPt := StrgLg-1 downto 0 do begin               { proceed from right to left }
  299.       TheByte := byte (Strg [StrgPt]);                     {  encrypt similar as in first pass }
  300.       Exor (StrgLg, TheByte);                              { xor it using string length...}
  301.       if (StrgPt = StrgLg - 1) then                        { If BOS }
  302.         Exor ($2a, TheByte)                                {...a constant...}
  303.       else begin
  304.         Exor (StrgPt, TheByte);                            {...actual string pointer...}
  305.         Exor (byte (Strg [StrgPt+1]), TheByte);            {...Next character }
  306.         end;
  307.       Strg [StrgPt] := char (TheByte);                     { store encrypted byte back }
  308.       end; { for };
  309. end;
  310.  
  311.  
  312. Procedure TWindowsUserInfo.EncryptCString(S : PChar);
  313. Begin
  314.   WinEncrypt(S);
  315. end;
  316.  
  317. Function TWindowsUserInfo.EncryptString(const S : String) : string;
  318. begin
  319.   Result := S;
  320.   if Result[0] < #254 then begin
  321.     Result[Integer(Result[0]) + 1] := Chr(0);
  322.     WinEncrypt(@Result[1]);
  323.     end;
  324. end;
  325.  
  326. {-----------------------------------------------------------------------------------------}
  327. {                                                                                         }
  328. {-----------------------------------------------------------------------------------------}
  329.  
  330. end.
  331.